package com.example.nettyclient.ui;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.nettyclient.R;
import com.example.nettyclient.base.BaseFragment;
import com.example.nettyclient.bean.MessageBean;
import com.example.nettyclient.databinding.CellNormalBinding;
import com.example.nettyclient.databinding.FragmentMainBinding;
import com.example.nettyclient.view.recycleview.MyAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends BaseFragment {
    FragmentMainBinding fragmentMainBinding;
    List<MessageBean> allMessage;
    MyAdapter myAdapter;

    public MainFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //return inflater.inflate(R.layout.fragment_main, container, false);
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        fragmentMainBinding = FragmentMainBinding.bind(view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        allMessage = new ArrayList<>();
        mSharedViewModel.allMessages.setValue(allMessage);
        //allMessage.add(new MessageBean("123456", 0 ));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        fragmentMainBinding.recycle.setLayoutManager(linearLayoutManager);
        myAdapter = new MyAdapter();
        fragmentMainBinding.recycle.setAdapter(myAdapter);

        fragmentMainBinding.send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = fragmentMainBinding.input.getText().toString();
                if(!input.equals("")){
                    allMessage = mSharedViewModel.allMessages.getValue();
                    allMessage.add(new MessageBean(input, 0));
                    mSharedViewModel.allMessages.setValue(allMessage);

                    fragmentMainBinding.input.setText("");
                    fragmentMainBinding.input.clearFocus();
                }
            }
        });

        mSharedViewModel.allMessages.observe(getViewLifecycleOwner(), new Observer<List<MessageBean>>() {
            @Override
            public void onChanged(List<MessageBean> messageBeans) {
                myAdapter.setAllMessages(allMessage);
                myAdapter.notifyDataSetChanged();
            }
        });
    }
}
